/*********************参数校验区****************************/
/*标题校验*/
$.validator.addMethod("titleCheck", function (value, element) {
    var tel = /^[A-Za-z0-9\u4e00-\u9fa5]{1,20}$/;
    return this.optional(element) || (tel.test(value));
}, "输入有误");

/*轮播顺序*/
$.validator.addMethod("seqCheck", function (value, element) {
    //var re = /^[0-9]+.?[0-9]*$/;
    return this.optional(element) || (Number(value) >= 0 && Number(value) < 100);
}, "输入有误");

// 选择图片
function changImg(e) {
    var image = new Image();
    var file = $("#file")[0].files[0];
    // 判断选择的文件是否是图片
    if (!(/^image\/.*$/i.test(file.type))) {
        layer.msg('请上传图片!', {icon: 5, time: 3000});
        $("#file").val("")
        return;
    }
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            layer.msg('请上传图片!', {icon: 5, time: 2000});
            $("#file").val("")
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var image = new Image();
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function (e) {
            image.src = e.target.result;
            image.onload = function () {
                if (file.size > 5 * 1024 * 1024) {
                    layer.msg('图片不得大于5M!', {icon: 6, time: 2000});
                    $("#file").val("")
                    return;
                } else {
                    var element = $(e.target).next();
                    if (this.width != 1920 || this.height != 530) {
                        layer.msg('图片大小限制为1920*530!', {icon: 6, time: 2000});
                        r = 1
                        $("#file").val("")
                        return;
                    } else {
                        $("#myImg").css({
                            width: this.width / 5 + "px",
                            height: this.height / 5 + "px",
                            cursor: "pointer"
                        });
                        $("#myImg").attr("src", e.target.result);
                    }
                }
            };
        }
    }
}

/***********************方法区***************************/
var imgUrl;
/*编辑 填充表单*/
$(function () {
    if (window.parent.pictureId != null) {
        $.ajax({
            url: root.sroreinter.get_slide_pictureByid + '/' + window.parent.pictureId,
            cache: false,  //禁用缓存
            type: "GET",
            // 告诉jQuery不要去处理发送的数据
            processData: false,
            // 告诉jQuery不要去设置Content-Type请求头
            contentType: false,
            success: function (data) {
                if (data) {
                    $("#title").val(data.title);
                    imgUrl = data.url;
                    $("#myImg").attr("src", data.url);
                    $("#jump_url").val(data.jump_url);
                    $("#start_time").val(data.start_time);
                    $("#end_time").val(data.end_time);
                    $("#seq").val(data.seq);
                    // 编辑时不需要设置必填
                    $("#file").removeAttr("required");
                }
            }
        });
    }
});
// 保存信息更新或修改
$("#form-add").validate({
    rules: {
        title: "titleCheck",
        seq: "seqCheck",
        jump_url: {
            url: true
        }
    },
    //提交表单数据
    submitHandler: function (form) {
        var loader = layer.load(0, {
            shade: [0.8, '#070002'] //0.1透明度的白色背景
        });
        var file = $("#file")[0].files[0];
        var formData = new FormData();
        // 有文件时
        if (file) {
            // 更新文件路径
            uploadFile(formData);
        } else if (window.parent.pictureId != null && !file) {
            // 更新操作时未上传文件
            formData.append("url", imgUrl);
        }
        /*获取登陆信息*/
        var quzhi = JSON.parse(web.readCookie("loginData"))
        formData.append("title", $("#title").val());
        formData.append("jump_url", $("#jump_url").val());
        formData.append("start_time", $('#start_time').val());
        formData.append("end_time", $('#end_time').val());
        formData.append("seq", $("#seq").val());
        /*数据入库*/
        if (window.parent.pictureId) {
            formData.append("id", window.parent.pictureId);
            formData.append("modifier_id", quzhi.user.user_ID);
            formData.append("modifier_name", quzhi.user.username);
            updatePicture(formData);
        } else {
            formData.append("creator_name", quzhi.user.username);
            formData.append("creator_id", quzhi.user.user_ID);
            addPicture(formData);
        }
        setTimeout(function () {
            layer.close(loader);
        }, 3000);
    }
})

/*关闭详情页*/
function removeIframe() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

/*调用图片上传基础服务*/
function uploadFile(formData) {
    var fileFormData = new FormData();
    fileFormData.append("file", $("#file")[0].files[0]);
    /* 同步保存图片 */
    $.ajax({
        url: root.sroreinter.save_slide_picture,
        cache: false,  //禁用缓存
        type: "POST",
        async: false,
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        data: fileFormData,
        success: function (data) {
            if (data[0].errorCode == 0) {
                formData.append("url", data[0].fullPath);
            } else {
                layer.msg("基础服务异常: " + data[0].errorInfo, {icon: 5, time: 3000});
                return;
            }
        }
    });
}

/*新增图片信息*/
function addPicture(formData) {
    $.ajax({
        url: root.sroreinter.add_slide_picutre,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        async: false,
        data: formData,
        success: function (data) {
            if (data.errorCode == 0) {
                layer.msg(data.errorMessage, {icon: 6, time: 3000});
                setTimeout(function () {
                    parent.comPageNum = 1;
                    parent.seahsdfs(1);
                    removeIframe();
                }, 3000);
            } else {
                layer.msg(data.errorMessage, {icon: 5, time: 3000});
            }
        }
    });
}

/*更新图片信息*/
function updatePicture(formData) {
    $.ajax({
        url: root.sroreinter.update_slide_picutre,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        async: false,
        data: formData,
        success: function (data) {
            if (data.errorCode == 0) {
                layer.msg(data.errorMessage, {icon: 6, time: 3000});
                setTimeout(function () {
                    parent.seahsdfs(parent.comPageNum);
                    removeIframe();
                }, 3000);
            } else {
                layer.msg(data.errorMessage, {icon: 5, time: 3000});
            }
        }
    });
}

